package com.olda.play.system.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.olda.play.enums.ResponseCode;
import com.olda.play.model.DropOption;
import com.olda.play.rpc.CommonResponseDto;
import com.olda.play.system.biz.SysRoleBizService;
import com.olda.play.system.dto.SysRoleDto;
import com.olda.play.system.model.po.SysRole;
import com.olda.play.system.service.SysRoleService;
import com.olda.play.system.vo.SysDeptVo;
import com.olda.play.system.vo.SysRoleVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色业务接口
 * @author oldA
 */
@Service
@RequestMapping(value =  "/role")
@RestController
public class SysRoleServiceImpl implements SysRoleService {

    
    @Autowired
    private SysRoleBizService sysRoleBizService;

    /**
     * 分页获取角色列表
     * @return
     */
    @Override
    @RequestMapping(value = "/querySysRoleList",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<List<SysRoleDto>> querySysRoleList(@RequestBody SysRoleVo param) {
        return sysRoleBizService.querySysRoleList(param);
    }

    /**
     * 新增或修改角色
     * @param param
     * @return
     */
    @Override
    @RequestMapping(value = "/addOrUpdateRole",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public CommonResponseDto<Boolean> addOrUpdateRole(@RequestBody SysRoleVo param) {
        if(param==null){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        Long id=param.getId();
        if(id==null){
            SysRole sysRoleOld=sysRoleBizService.getSysRoleByRoleCode(param.getCode());
            if(sysRoleOld!=null){
                return CommonResponseDto.error(ResponseCode.MORE_ROLE);
            }
            SysRole SysRole=BeanUtil.copyProperties(param,SysRole.class);
            sysRoleBizService.save(SysRole);
        }else{
            SysRole SysRole=BeanUtil.copyProperties(param,SysRole.class);
            sysRoleBizService.updateSysRole(SysRole);
        }
        return CommonResponseDto.success(true);
    }

    /**
     * 给角色分配权限
     * @param param
     * @return
     */
    @Override
    @RequestMapping(value = "/allotPerms",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public CommonResponseDto<Boolean> allotPerms(@RequestBody SysRoleVo param) {
        if(param==null||param.getMenuIds()==null){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        sysRoleBizService.allotPerms(param.getId(),param.getMenuIds());
        return CommonResponseDto.success(true);
    }

    /**
     * 删除角色
     * @param id 角色ID
     * @return
     */
    @Override
    @RequestMapping(value = "/deleteSysRoleById",method = RequestMethod.GET ,produces="application/json;charset=UTF-8" )
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public CommonResponseDto<Boolean> deleteByIds(Long id) {
        sysRoleBizService.deleteRoleMenuById(id,0);
        return CommonResponseDto.success(true);
    }


    /**
     * 新增用户时获取下拉角色数据
     * @return
     */
    @Override
    @RequestMapping(value = "/lisetRoleOptions",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<List<DropOption>> lisetRoleOptions() {
        List<DropOption> dropOptions =new ArrayList<>();
        SysDeptVo param=new SysDeptVo();
        param.setStatus(1);
        List<SysRoleDto> list=sysRoleBizService.querySysRoleListNoPage(param);
        if(list.size()>0){
            list.forEach(item ->{
                dropOptions.add(new DropOption(item.getId(),item.getName()));
            });
        }
        return CommonResponseDto.success(dropOptions);
    }
}
